<template>
  <div>
    <van-row>
      <van-col span="8"></van-col>
      <van-col span="8">悄悄告诉你我其实最优秀了</van-col>
      <van-col span="8"></van-col>
    </van-row>
    <van-form @submit="login">
      <van-field
        v-model="form.mobile"
        name="手机号"
        label="手机号"
        placeholder="请输入手机号"
        :rules="formrules.mobile"
      />
      <van-field
        v-model="form.code"
        type="password"
        name="密码"
        label="密码"
        placeholder="请输入密码"
        :rules="formrules.code"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import { UserApi } from "/p5/P5_认真学习/headline/src/api/userAPI";
export default {
  data() {
    return {
      form: {
        mobile: "13888888888",
        code: "246810",
      },
      formrules: {
        mobile: [
          { required: true, message: "请输入手机号", trigger: "onBlur" },
          {
            message: "请填写正确的手机号",
            pattern: /^1\d{10}$/,
            trigger: "onBlur",
          },
        ],
        code: [{ required: true, message: "请输入密码", trigger: "onBlur" }],
      },
    };
  },
  methods: {
    ...mapMutations(["saveTokenInfo"]),
    async login() {
      var { data: res } = await UserApi(this.form);
      console.log("登录的数据", res);
      // this.$store.commit("getToken", res.data);
      this.saveTokenInfo(res.data);
      if (res.message === "OK") {
        this.$router.push("/");
      }
    },
  },
};
</script>

<style></style>
